Ext.ns('AIR2.Upload');
/***************
 * File uploader modal
 *
 * Upload and import a CSV file into the Tank, using a single modal window.
 * Upload and validation will be done synchronously.
 *
 * @cfg {HTMLElement} originEl (optional) origin element to animate from
 * @cfg {Object} tankRec (optional) tank record to load
 *
 */
AIR2.Upload.Modal = function(cfg) {
    var upload = AIR2.Upload.FileForm();
    var preview = AIR2.Upload.PreviewGrid();
    var importer = AIR2.Upload.Importer();

    var w = new AIR2.UI.Window({
        title: 'Upload CSV File',
        closeAction: 'close',
        iconCls: 'air2-icon-upload',
        width: 700,
        height: 450,
        formAutoHeight: true,
        layout: 'column',
        items: [{
            xtype: 'box',
            columnWidth: .05,
            style: 'height: 450px',
            html: '&nbsp'
        },{
            xtype: 'container',
            columnWidth: .95,
            items: [upload, preview, importer]
        }],
        setStep: function(num) {
            // TODO: there seems to be an Ext problem where element positions
            // won't get returned correctly... for now, just pause
            var alignEl = w.get(1).get(num - 1).el;
            if (alignEl.getX() == 0) {
                this.setStep.defer(100, this, [num]);
            }
            else {
                var animate = {
                    duration: .1,
                    callback: function() {w.stepEl.update(num)}
                };
                if (!w.stepEl) {
                    w.stepEl = w.get(0).el.createChild({
                        style: 'position:absolute; font-size:24px; font-weight:bold; color:#666'
                    });
                    w.stepEl.update(num);
                    w.stepEl.sequenceFx();
                    animate = false; // no initial animation
                }
                w.stepEl.alignTo(alignEl, 'r-l', [0, 0], animate);
            }
        },
        setRecData: function(data) {
            var meta = (data) ? Ext.decode(data.tank_meta) : {};

            // find the stepnum
            if (meta.submit_success) {
                this.setStep(3); // done!
            }
            else if (meta.valid_header) {
                this.setStep(3); // step 3, but not done
            }
            else if (meta.valid_file) {
                this.setStep(2); // uploaded, but header invalid
            }
            else {
                this.setStep(1); // no file!
            }

            if (data) {
                upload.setTankData(data);
                preview.setTankData(data);
                importer.setTankData(data);
            }
        }
    });

    upload.on('afterupload', function(success, data) {
        w.setRecData(data.radix);
    });
    preview.on('afterpreview', function(success, data) {
        w.setRecData(data.radix);
    }, this);
    importer.on('aftersubmit', function(success, data) {
        w.setRecData(data.radix);
    });

    w.on('afterrender', function() {
        w.setRecData(cfg.tankRec ? cfg.tankRec.data : null);
    });

    if (cfg.originEl) w.show(cfg.originEl);
    else w.show();
    return w;
};